package com.stedu.controller;

import com.stedu.bean.RespBean;
import com.stedu.bean.Teacher;
import com.stedu.service.StudentService;
import com.stedu.service.TeacherAndStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@CrossOrigin//跨域问题
@RestController
@RequestMapping("/admin/teacherAndStudent")
public class TeacherAndStudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherAndStudentService teacherAndStudentService;

    //查询所有的老师信息
    @GetMapping
    public RespBean selectAll() {
        List<Teacher> selectAll = teacherAndStudentService.selectAll();
        return RespBean.ok("", selectAll);
    }

    //获取用户信息
    @GetMapping("/allStudent/{tid}")
    public RespBean allStudent(@PathVariable("tid")Integer tid) {
        List<Map<String, Object>> stuentMapList = studentService.selectBySname(null,null)
                .stream()
                .map(e -> {
                    Map<String, Object> StudentMap = new HashMap<>();
                    StudentMap.put("key", e.getSid());
                    StudentMap.put("label", e.getCollege().getProfessional() + "-" + e.getSname());
                    return StudentMap;
                }).collect(Collectors.toList());

        //获取已经分配的学生的tid
        List<Integer> sids = teacherAndStudentService.selectSidByTid(tid);

        Map<String, Object> map = new HashMap<>();
        map.put("allStudent",stuentMapList);
        map.put("selectedSids", sids);
        return RespBean.ok("", map);
    }
    @PostMapping("/tidAndSid")
    public RespBean insertPidAndEid(@RequestBody Map<String,Object>map) {
        System.out.println(map);//例如：{tid=1, sids=[4, 6]}
        Integer tid = (Integer) map.get("tid");
        List<Integer> sidsList = (List<Integer>)map.get("sids");
        Integer[]sids=new Integer[sidsList.size()];
        for (int i = 0; i < sidsList.size(); i++) {
            int sid=sidsList.get(i);
            sids[i]=new Integer(sid+"");
        }
        teacherAndStudentService.InsertTidAndSid(tid, sids);

        return RespBean.ok("分配员工成功");
    }
}
